package feizhou.untils.tools;

import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author feizhou
 */

public class StringUtil {
    public static String nullSign = "";

    /**
     * 过滤空NULL,将控制转换为 nullSign
     *
     * @param obj
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 10:53
     */

    public static String FilterNull(Object obj) {
        if (obj == null) {
            return nullSign;
        }
        if (obj instanceof String) {
            String str = (String) obj;
            if ("null".equals(str) || str.length() == 0) {
                return nullSign;
            }
        } else if (obj instanceof Collection) {
            Collection col = (Collection) obj;
            if (col.isEmpty()) {
                return nullSign;
            }
        } else if (obj instanceof Map) {
            Map temp = (Map) obj;
            if (temp.isEmpty()) {
                return nullSign;
            }
        } else if (obj instanceof Object[]) {
            Object[] array = (Object[]) obj;
            if (array.length <= 0) {
                return nullSign;
            }
        }
        return obj.toString().trim();
    }

    /**
     * 判断是否为空
     *
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 10:54
     */
    public static boolean isEmpty(Object o) {
        if (nullSign.equals(FilterNull(o))) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否非空
     *
     * @param o
     * @return boolean
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 14:17
     */
    public static boolean isNotEmpty(Object o) {
        if (isEmpty(o)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 首字母大小写转换
     *
     * @param str  字符串
     * @param type 0 转小写，1转大写
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 10:56
     */
    public static String caseFirstChar(String str, int type) {
        char[] cs = str.toCharArray();
        if (type == 0 && Character.isUpperCase(cs[0])) {
            cs[0] += 32;
            return String.valueOf(cs);
        }

        if (type == 1 && Character.isLowerCase(cs[0])) {
            cs[0] -= 32;
            return String.valueOf(cs);
        }
        return str;
    }

    /**
     * 将驼峰 替换为 下划线,驼峰字母变小写
     *
     * @param str
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 16:28
     */
    public static String humpToUnderline_lowercase(String str) {
        if (isEmpty(str)) {
            return nullSign;
        }
        int size = str.length();
        char[] chars = str.toCharArray();
        StringBuilder sb = new StringBuilder(size * 3 / 2 + 1);
        char c;
        for (int i = 0; i < size; i++) {
            c = chars[i];
            // 大写 转 下划线
            if (isUppercase(c)) {
                sb.append('_').append(toLowercase(c));
            } else {
                // 小写不改动
                sb.append(c);
            }
        }
        return sb.charAt(0) == '_' ? sb.substring(1) : sb.toString();
    }

    /**
     * 将驼峰 替换为 下划线,驼峰字母变大写
     *
     * @param str
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 16:28
     */
    public static String humpToUnderline_upercase(String str) {
        if (isEmpty(str)) {
            return nullSign;
        }
        int size = str.length();
        char[] chars = str.toCharArray();
        StringBuilder sb = new StringBuilder(size * 3 / 2 + 1);
        char c;
        for (int i = 0; i < size; i++) {
            c = chars[i];
            // 大写 转 下划线
            if (isUppercase(c)) {
                sb.append('_').append(c);
            } else {
                // 小写不改动
                sb.append(c);
            }
        }
        return sb.charAt(0) == '_' ? sb.substring(1) : sb.toString();
    }

    /**
     * 将 下划线 替换为 驼峰
     *
     * @param str
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 16:51
     */
    public static String underlineToHump(String str) {
        // 匹配下划线开头的字母
        final Matcher matcher = Pattern.compile("_[a-z]").matcher(str);
        StringBuilder builder = new StringBuilder(str);
        // 将匹配的字母改为大写，并去掉下划线
        for (int i = 0; matcher.find(); i++) {
            builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase());
        }
        // 首字母大写改为小写
        char c = builder.charAt(0);
        if (isUppercase(c)) {
            builder.replace(0, 1, String.valueOf(toLowercase(c)));
        }
        return builder.toString();
    }

    /**
     * 字符是否大写
     *
     * @param
     * @return boolean
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 16:20
     */
    public static boolean isUppercase(char c) {
        return (c >= 'A') && (c <= 'Z');
    }

    /**
     * 字符是否小写
     *
     * @param
     * @return boolean
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 16:20
     */
    public static boolean isLowercase(char c) {
        return (c >= 'a') && (c <= 'z');
    }

    /**
     * 字符转大写
     *
     * @param c
     * @return char
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 16:22
     */
    public static char toUppercase(char c) {
        if (isLowercase(c)) {
            c -= 32;
        }
        return c;
    }

    /**
     * 字符串转大写
     *
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 16:22
     */
    public static String toUppercase(String str) {
        return str.toUpperCase();
    }

    /**
     * 字符转小写
     *
     * @param c
     * @return char
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 16:22
     */
    public static char toLowercase(char c) {
        if (isUppercase(c)) {
            c += 32;
        }
        return c;
    }

    /**
     * 字符串转小写
     *
     * @return char
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 16:22
     */
    public static String toLowercase(String str) {

        return str.toLowerCase();
    }

    /**
     * 根据指定的位置获取单个字符
     *
     * @param str
     * @param index
     * @return
     */
    public static String getCharByIndex(String str, int index) {
        if (StringUtil.isEmpty(str) || index < 0 || index > (str.length() - 1)) {
            return null;
        }
        return str.substring(index, ++index);
    }

    /**
     * 替换字符串中指定位置的单个字符
     *
     * @param str     原字符串
     * @param charStr 需要替换的单个字符
     * @param index   位置
     * @return
     */
    public static String replaceCharByIndex(String str, char charStr, int index) {
        if (StringUtil.isEmpty(str) || index < 0 || index > (str.length() - 1)) {
            return str;
        }
        if (index == 0) {
            return charStr + str.substring(1, str.length());
        }
        if (index == str.length()) {
            return str.substring(0, index - 1) + charStr;
        }
        String pre = str.substring(0, index);
        String tail = str.substring(++index, str.length());
        return pre + charStr + tail;
    }

    /**
     * 字符串转换unicode
     *
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/7/27 16:09
     */
    public static String string2Unicode(String string) {

        StringBuffer unicode = new StringBuffer();

        for (int i = 0; i < string.length(); i++) {
            // 取出每一个字符
            char c = string.charAt(i);
            // 转换为unicode
            unicode.append("\\u" + Integer.toHexString(c));
        }
        return unicode.toString();
    }

    /**
     * 换行符 转换
     * string：要转换的字符串
     * type: 类型
     * 1: CRLF转换成LF
     * 2: LF转换成CRLF
     *
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/7/27 16:09
     */
    public static String lineSeparatorsConvert(String str, int type) {
        if (type == 1) {
            return str.replaceAll("\r\n", "\n");
        }
        if (type == 2) {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < str.length(); i++) {
                //当遇到LF时，我们在StringBuilder中加入一个CR
                if (str.charAt(i) == '\n' && str.charAt(i - 1) != '\r') {
                    result.append('\r');
                }
                result.append(str.charAt(i));
            }
            str = result.toString();
            return str;
        }
        return str;
    }


    /**
     * 将 下划线 替换为 驼峰,驼峰字母变小写
     *
     * @param str
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 16:51
     */
    public static String underlineToHump_lowercase(String str) {
        // 匹配下划线开头的字母
        final Matcher matcher = Pattern.compile("_[a-z]").matcher(str);
        StringBuilder builder = new StringBuilder(str);
        // 将匹配的字母改为大写，并去掉下划线
        for (int i = 0; matcher.find(); i++) {
            builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase());
        }
        // 首字母大写改为小写
        char c = builder.charAt(0);
        if (isUppercase(c)) {
            builder.replace(0, 1, String.valueOf(toLowercase(c)));
        }
        return builder.toString();
    }

    /**
     * 将 下划线 替换为 驼峰,驼峰字母变大写
     *
     * @param str
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/5 16:51
     */
    public static String underlineToHump_upercase(String str) {
        // 匹配下划线开头的字母
        final Matcher matcher = Pattern.compile("_[a-z]").matcher(str);
        StringBuilder builder = new StringBuilder(str);
        // 将匹配的字母改为大写，并去掉下划线
        for (int i = 0; matcher.find(); i++) {
            builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase());
        }
        // 首字母小写改为大写
        char c = builder.charAt(0);
        if (isLowercase(c)) {
            builder.replace(0, 1, String.valueOf(toUppercase(c)));
        }
        return builder.toString();
    }


    public static void main(String[] args) {
        String str = "这是一段有LF换行符的文本\n这是下一行";
        String str2 = "这是一段有CRLF换行符的文本\r\n这是下一行";


        System.out.println(lineSeparatorsConvert(str2, 2));
//        System.out.println(isUppercase('a'));
//        System.out.println(toLowercase('a'));
//        System.out.println(toUppercase('a'));
//        System.out.println(humpToUnderline_lowercase("aaaSaaa"));
//        System.out.println(humpToUnderline_upercase("aaaSaaa"));
//        System.out.println(underlineToHump("aaa_saaa"));


//        System.out.println(replaceCharByIndex("123456", '1', 2));
//        System.out.println(replace("123456", 2));
    }

}